// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {

        RigidBody:null,

        isTochGround:false,

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        //获取自己的碰撞体
        this.RigidBody = this.getComponent(cc.RigidBody);
    },

    start () {

    },

    update (dt) {
        this.BallgoHome();
    },


    // 只在两个碰撞体开始接触时被调用一次
    /**
     * 检测是否碰到地面
     * @param contact
     * @param selfCollider
     * @param otherCollider
     */
    onBeginContact: function (contact, selfCollider, otherCollider) {
        let selfName = selfCollider.node.name;
        let otherName = otherCollider.node.name;
        if (otherName == 'ground'){
            this.isTochGround = true;
        }

    },


    /**
     * 小球返回上面
     * @constructor
     */
    BallgoHome(){
        let index = require("index");
        if (this.isTochGround){
            this.isTochGround = false;
            //关闭碰撞体
            this.RigidBody.active = false;
            //设置自身速度为0
            this.RigidBody.linearVelocity  = cc.Vec2.ZERO;


            //定义一个位置数据
            let pathPos = [];

            if (this.node.x < 0){
                //从左边上去
                pathPos.push(cc.v2(this.node.position));
                pathPos.push(cc.v2(-336,-466.5));
                pathPos.push(cc.v2(-336,599));
                pathPos.push(cc.v2(-163,546));
            }else {
                //从右边上去
                pathPos.push(cc.v2(this.node.position));
                pathPos.push(cc.v2(336.7,-470));
                pathPos.push(cc.v2(337,605));
                pathPos.push(cc.v2(168,545));
            }

            this.node.runAction(cc.sequence(
                cc.cardinalSplineTo(2,pathPos,0.8),
                cc.callFunc(function () {
                    this.RigidBody.active = true;
                    index.Smallone(this);
                }.bind(this)),
            ),0.8);
        }
    },







});


